﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using PaceR.Core.Interfaces;
using PaceR.Web.WebFactories;
using PaceR.Web.WebInterfaces;
using PaceR.Web.WebUtilities;

namespace PaceR.Web.UI.Controls
{
    [ToolboxData("<{0}:MessageDisplay runat=server></{0}:MessageDisplay>")]
    public class MessageDisplay : WebControl
    {
        protected List<IResultMessage> Messages { get; private set; }

        public MessageDisplay()
        {
            Messages = new List<IResultMessage>();
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            if (Messages.Count > 0)
            {
                var messageRenderer = HtmlRendererFactoryUtil.Factory.GetMessageRenderer();
                //Errors
                RenderErrors(output, messageRenderer);
                //Warnings
                RenderWarnings(output, messageRenderer);
                //Information
                RenderInformation(output, messageRenderer);
            }
        }

        protected virtual void RenderErrors(HtmlTextWriter output, IMessageRenderer messageRenderer)
        {
            IEnumerable<IResultMessage> errors = Messages.Where(m => m.MessageType == ResultMessageTypes.Error);
            if (errors.Count() > 0)
            {
                messageRenderer.RenderMessages(output, errors, this.CssClass, ResultMessageTypes.Error);
            }
        }

        protected virtual void RenderWarnings(HtmlTextWriter output, IMessageRenderer messageRenderer)
        {
            IEnumerable<IResultMessage> warnings = Messages.Where(m => m.MessageType == ResultMessageTypes.Warning);
            if (warnings.Count() > 0)
            {
                messageRenderer.RenderMessages(output, warnings, this.CssClass, ResultMessageTypes.Warning);
            }
        }

        protected virtual void RenderInformation(HtmlTextWriter output, IMessageRenderer messageRenderer)
        {
            IEnumerable<IResultMessage> info = Messages.Where(m => m.MessageType == ResultMessageTypes.Information);
            if (info.Count() > 0)
            {
                messageRenderer.RenderMessages(output, info, this.CssClass, ResultMessageTypes.Information);
            }
        }

        public virtual void AddMessage(IResultMessage message)
        {
            if (message != null)
                Messages.Add(message);
        }

        public virtual void AddMessages(IEnumerable<IResultMessage> messages)
        {
            if (messages != null)
                Messages.AddRange(messages);
        }

        public virtual bool HasErrors()
        {
            return Messages.Where(m => m.MessageType == ResultMessageTypes.Error).Count() > 0;
        }

        public virtual bool HasWarnings()
        {
            return Messages.Where(m => m.MessageType == ResultMessageTypes.Warning).Count() > 0;
        }

        public virtual bool HasWarningsOrErrors()
        {
            return HasErrors() || HasWarnings();
        }
    }
}
